#!/usr/bin/env bash
#
# Delete the "guest" default user and replace it with a Zulip user
# with a real password
set -e
set -x

RABBITMQ_HOST=$("$(dirname "$0")/../get-django-setting" RABBITMQ_HOST)
RABBITMQ_USERNAME=$("$(dirname "$0")/../get-django-setting" RABBITMQ_USERNAME)
RABBITMQ_PASSWORD=$("$(dirname "$0")/../get-django-setting" RABBITMQ_PASSWORD)
sudo rabbitmqctl -n "$RABBITMQ_HOST" delete_user "$RABBITMQ_USERNAME" || true
sudo rabbitmqctl -n "$RABBITMQ_HOST" delete_user zulip || true
sudo rabbitmqctl -n "$RABBITMQ_HOST" delete_user guest || true
sudo rabbitmqctl -n "$RABBITMQ_HOST" add_user "$RABBITMQ_USERNAME" "$RABBITMQ_PASSWORD"
sudo rabbitmqctl -n "$RABBITMQ_HOST" set_user_tags "$RABBITMQ_USERNAME" administrator
sudo rabbitmqctl -n "$RABBITMQ_HOST" set_permissions -p / "$RABBITMQ_USERNAME" '.*' '.*' '.*'
